package com.example.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.fragment.adapter.ExerciseAdapter;
import com.example.fragment.dummy.ExerciseContent;

public class ExerciseFragment extends Fragment {
    private static final String ARG_PARAM = "param";

    private String mParam;

    private DataCallback callback;

    public ExerciseFragment() {
        // Required empty public constructor
    }

    public static ExerciseFragment newInstance(String param) {
        ExerciseFragment fragment = new ExerciseFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM, param);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam = getArguments().getString(ARG_PARAM);
        }
        callback = (DataCallback) getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_execise, container, false);
        // 设置 Adapter
        if (view instanceof RecyclerView) {
            RecyclerView recyclerView = (RecyclerView) view;
            recyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));
            ExerciseAdapter adapter = new ExerciseAdapter(ExerciseContent.ITEMS);
            recyclerView.setAdapter(adapter);
            adapter.setOnItemClickListener(new ExerciseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {

                }
            });

        }
        callback.putTitle(mParam);
        return view;
    }

    public interface DataCallback {
        void putTitle(String title);
    }
}